const mongoose = require('mongoose');

const dbPath = 'mongodb://127.0.0.1:27017/rh15'

// 数据库连接
mongoose.connect(dbPath);

mongoose.connection.on('open', async () => {
  console.log('数据库连接成功');

  // // 创建数据
  // await UserModel.create({
  //   username: 'abc',
  //   password: 'admin'
  // })

  // 删除数据
  // await UserModel.deleteOne({ _id: '641177f533bc0a58bab0081a' })
  // await UserModel.findByIdAndDelete('641177f533bc0a58bab0081a')

  // // 修改
  // await UserModel.findByIdAndUpdate('64117873352df64bcfabb8b2', {
  //   username: 'ttt',
  //   password: '123456'
  // })

  // // 查询数据
  // let users = await UserModel.find({})
  // console.log(users);
})

mongoose.connection.on('error', () => {
  console.log('数据库连接失败');
  process.exit();
})
